package com.example.gymnasiumsystem.servlet;

import com.example.gymnasiumsystem.dao.GymInfoDAO;
import com.example.gymnasiumsystem.enity.GymInfo;
import com.example.gymnasiumsystem.enity.UserInfo;
import com.example.gymnasiumsystem.service.impl.GymServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addGym")
public class AddGym extends HttpServlet {
    private GymInfoDAO gymInfoDAO=new GymInfoDAO();
    private GymServiceImpl service=new GymServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");  // 设置请求字符编码为UTF-8
        resp.setContentType("text/html;charset=UTF-8");  // 设置响应字符编码为UTF-8
        UserInfo userInfo1=(UserInfo) req.getSession().getAttribute("user");
        if(userInfo1.getIsAdmin()==0){
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("抱歉你没有这个权限");
            return;
        }
        String gymName=req.getParameter("gymName");
        GymInfo gymInfo=gymInfoDAO.getGymByName(gymName);
        if(gymInfo!=null){
            resp.sendRedirect("admin/gymList.jsp");
            return;
        }
        gymInfo=new GymInfo();
        gymInfo.setGymName(gymName);
        String gymLocation=req.getParameter("gymLocation");
        String gymDescription=req.getParameter("gymDescription");
        gymInfo.setGymDescription(gymDescription);
        gymInfo.setGymLocation(gymLocation);
        gymInfoDAO.addGym(gymInfo);
        resp.sendRedirect("admin/gymList.jsp");
    }
}
